<?php
/**
 * Product template attributes values. Values of templates' attributes 
 * product is assigned to.   
 * 
 * @author Konstantin Shamko <konstantin.shamko@gmail.com> 
 * @version 0.0.1
 * @copyright  Copyright (c) 2009 Konstantin Shamko
 * @package Products Module
 * @license  New BSD License
 *
 */
class Models_Products_AttributeValues extends Bel_Model_Abstract  {
	
	
	/**
	 * Array of many-to-one references with products  & templates attributes.
	 * Realtion between products and templates attributes is many-to-many
	 *
	 * @var array
	 */	
	protected $_referenceMap = array (
								 'Product' => 
										array ('columns' => 'product_id',
											   'refTableClass' => 'Models_Products',
								   			   'refColumns' => 'product_id', 
											   'onDelete' => self::CASCADE
								   			   ),
								 'TemplateAttr' => 
										array ('columns' => 'template_attribute_id',
											   'refTableClass' => 'Models_ProductTemplates_Attributes',
								   			   'refColumns' => 'template_attribute_id',
											   'onDelete' => self::CASCADE 
								   			   ),
								  );	
	
	
	/**
	 * Constructor. Sets table name & primary key
	 *
	 * @param array $array - see Zend_Db_Table_Abstract for details
	 */
	public function __construct($array = array()) {
		$this->_name = TABLE_PREFIX . 'product_attribute_values';
		$this->_primary = array ('product_id','template_attribute_id' );
		parent::__construct ( $array );
	
	}	
	
	/**
	 * Inserts template attribute values for product
	 *
	 * @param integer $productId - product ID
	 * @param array $templateValues - array with template attribute id as key and attribute value as value
	 */
	public function addValuesForProduct($productId, $templateValues){
		if($templateValues){
			foreach($templateValues as $templateId=>$value){
				$this->insert(array('product_id'=>$productId,'template_attribute_id'=>$templateId,'product_attribute_value'=>$value));
			}
		}
	}
	
	/**
	 * Updates template attribute values for product
	 *
	 * @param integer $productId - product ID
	 * @param array $templateValues - array with template attribute id as key and attribute value as value
	 */
	public function updateValuesForProduct($productId, $templateValues){
		$this->delete('product_id=\''.$productId.'\'');
		$this->addValuesForProduct($productId, $templateValues);
		/*if($templateValues){
			foreach($templateValues as $templateId=>$value){
				$this->insert(array('product_id'=>$productId,'template_attribute_id'=>$templateId,'product_attribute_value'=>$value));
			}
		}*/
	}
	
	/**
	 * Returns attibutes info, values, product template info by product ID
	 *
	 * @param integer $product_id
	 * @return array
	 */
	public function getAttributesByProduct($product_id){
		$oAttr = new Models_ProductAttributes();
		$oTemplateAttr = new Models_ProductTemplates_Attributes();
		
		$select = $this->_db
			->select()
			->from(array('a'=>$oAttr->info('name')))
			->joinInner(array('ta'=>$oTemplateAttr->info('name')),'a.attribute_id=ta.attribute_id')
			->joinLeft(array('av'=>$this->_name),'av.template_attribute_id=ta.template_attribute_id')
			->where('av.product_id='.$product_id)
			->order('ta.attribute_order DESC');
		

		$productAttributes = $this->_db->query($select)->fetchAll();	
		
		foreach ($productAttributes as &$productAttribute){
			$productAttribute = (array)$productAttribute;
		}	
		
		return $productAttributes;
	}
}
